package com.ttl.bc.ic.finishgoods.db;

import java.util.Date;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;

import com.ttl.bc.ap.stkrequest.BCStkRequestBO;
import com.ttl.bc.ap.stkrequest.BCStkRequestBean;
import com.ttl.bc.ap.stkrequest.BCStkRequestDAO;
import com.ttl.bc.ap.stkrequest.BCStkRequestForm;
import com.ttl.bc.ap.stkrequest.BCStkRequestSubBean;
import com.ttl.bc.ar.reqconfirm.bean.BCReqConfirmBean;

import com.ttl.bc.ar.reqconfirm.db.BCReqConfirmBO;
import com.ttl.bc.ar.reqconfirm.db.BCReqConfirmDAO;
import com.ttl.bc.ic.finishgoods.bean.BCFinishGoodsBean;
import com.ttl.bc.ic.finishgoods.bean.BCFinishGoodsSubBean;

import com.ttl.bc.processstock.bean.ProcessStockBean;
import com.ttl.bc.processstock.db.ProcessStockDAO;
import com.ttl.bc.stock.request2.action.BCStkIssue2Form;
import com.ttl.bc.stock.request2.bean.BCStkIssue2;
import com.ttl.bc.stock.request2.bean.BCStkIssueSub2;
import com.ttl.bc.stock.request2.db.BCStkIssue2BO;
import com.ttl.bc.stock.request2.db.BCStkIssue2DAO;
import com.ttl.common.util.CommonUtils;

public class BCFinishGoodsBO {

	public boolean add(ActionForm form, HttpServletRequest request,
			HttpServletResponse response) {

//		BCStkRequestForm reqForm = setForm(request);//(BCStkIssue2Form)form;
//		
//		BCIssueTypeBean issueType = new BCIssueTypeBean();
//		issueType.setCode(request.getParameter("issueType"));			
//		
//		BCProjectBean proj = new BCProjectBean();
//		proj.setCode(reqForm.getProjectCode());
//		
//		BCStkRequestBean  stkReq = setRequestObject(reqForm);
//		stkReq.setIttl_userLevel((Integer)request.getSession().getAttribute("userLevel"));
//		stkReq.setIttl_status("0");
//		
//		BCStkRequestDAO dao = new BCStkRequestDAO();
//		String hql = "FROM BCStkRequestBean WHERE docNo = '"+stkReq.getDocNo()+"' ";
//		List al = dao.find(hql);
//		if(al!=null) {
//			dao.add(stkReq);
//		}else {
//			String maxDocNo = CommonUtils.getMaxStkRequestDocNo(stkReq.getDocNo().substring(0, 5));
//			stkReq.setDocNo(maxDocNo);
//			dao.add(stkReq);
//		}
//
//		String user 	 = (String)request.getSession().getAttribute("userName");
//		System.out.println(CommonUtils.getDateTimeForStamp()+" #user:"+user+" #use :BCStkRequestDAO #mode :add");
//		
//		BCReqConfirmBean reqConfirm = setReqConfirm(stkReq);
//		reqConfirm.setDocNo(stkReq.getDocNo());
//		reqConfirm.setIttl_userLevel((Integer)request.getSession().getAttribute("userLevel"));
//		reqConfirm.setIttl_status("0");
//		
//		BCReqConfirmDAO dao2 = new BCReqConfirmDAO();
//		dao2.add(reqConfirm);
//				
//		System.out.println(CommonUtils.getDateTimeForStamp()+" #user:"+user+" #use :BCReqConfirmDAO #mode :add");
		
		return true;
	}

	@SuppressWarnings("unchecked")
	public boolean addToFinishGoods_TTL2(HttpServletRequest request,
			HttpServletResponse response) {
		
		BCStkRequestForm reqForm = BCStkRequestBO.setForm(request);
		System.out.println(reqForm.getDocNo());
		request.setAttribute("docNoFind", reqForm.getDocNo().substring(9));
		
		BCStkRequestBean  stkReq = BCStkRequestBO.setRequestObject(reqForm);
				
		BCStkRequestDAO dao = new BCStkRequestDAO();
		dao.updateStatus(stkReq.getDocNo(),"4");//ใส่ ittl_status เป็น 4 เมื่อยืนยันรับของ
		 
		String user 	 = (String)request.getSession().getAttribute("userName");
		System.out.println(CommonUtils.getDateTimeForStamp()+" #user:"+user+" #use :BCStkRequestDAO #mode :updateStatus");
		
		BCReqConfirmBean reqComfirm = BCReqConfirmBO.setReqConfirm(stkReq);		
		BCReqConfirmDAO dao2 = new BCReqConfirmDAO();
		dao2.updateStatus(reqComfirm.getDocNo(), "4");//ใส่ ittl_status เป็น 4 เมื่อยืนยันรับของ
				
		System.out.println(CommonUtils.getDateTimeForStamp()+" #user:"+user+" #use :BCReqConfirmDAO #mode :updateStatus");	
		
		
		@SuppressWarnings("unused")
		String[] item_of_service = {"A-01", "A-02", "C-3",  "D-03", "D-04", "D-4",  "E-04", "E-05", "E-4",  "E-5",  "E-S-E",
				"F-04", "F-05", "F-06", "F-4",  "F-5",  "F-6",  "G-06", "G-08", "G-10", "G-11", "G-12", 
				"G-13", "G-14", "G-24", "GE",   "H-07", "H-08", "H-15", "HO",   "I-10", "I-16", "IM",
				"INT",  "J-11", "J-17", "K-14", "K-15", "K-16", "K-17", "K-18", "L-18", "L-19", "M-19",
				"M-21", "N-20", "N-22", "O-21", "P-22", "P-23", "Q-23", "R-24", "R-25", "S-25", "SE", "TREEX", "TRECO", 
				"SER-006","SES","ST",   "T-26", "U-27", "V-28", "Z-01", "Z-02", "Z-03", "Z-04", "Z-05", "Z-06"};
		
		if(request.getParameter("fgDocNo")!=null) {
			// สร้างรายการใหม่ลงใน table: BCFinishGoods (เพื่อเก็บไว้ออก report)
			BCFinishGoodsBean fg = setBCFishGoods(request,stkReq);
			
			new BCFinishGoodsDAO().add(fg);
	//		BCStkIssueBean issue = setBCStkIssue(request,stkReq);
	//		BCStkIssueDAO dao3 = new BCStkIssueDAO();
	//		System.out.println("add stkissue: "+dao3.add(issue));
	//
			System.out.println(CommonUtils.getDateTimeForStamp()+" #user:"+user+" #use :BCFinishGoodsDAO #mode :add "+
					  "#docNo:"+fg.getDocNo());
			
			
			// ใส่ข้อมูล itemCode รายการนั้นใน table: processStock 
			ProcessStockDAO p = new ProcessStockDAO();
	    	Iterator it = fg.getBcFinishGoodsSubs().iterator();
	    	
	        while(it.hasNext()) {
	        	BCFinishGoodsSubBean sub = (BCFinishGoodsSubBean)it.next();
	        	
	        	ProcessStockBean bean = new ProcessStockBean();
	        	bean.setItemCode(sub.getItemCode());
	        	bean.setDocDate(sub.getDocDate());
	        	bean.setProcessFlag(0);
	        	bean.setFlowStatus(1);
	        	bean.setProcessType(2);
	        	bean.setProcessCase(1);// สำหรับ BCStkIssue บันทึกเอกสารให้ =1, ลบเอกสารให้=3
	        	
	        	p.add(bean);
	        	
	    		System.out.println(CommonUtils.getDateTimeForStamp()+" #user:"+user+" #use :ProcessStockDAO #mode :add "+
	  				  "#docNo:"+fg.getDocNo() + " #item:"+sub.getItemCode());        	
	        } 
		}
         
		return true;
		
	}
	
	@SuppressWarnings("unchecked")
	public boolean addToFinishGoods_TTL2_stkReq(HttpServletRequest request,
			HttpServletResponse response) {
				
//		BCStkRequestForm reqForm = BCStkRequestBO.setForm(request);
		BCStkIssue2Form reqForm = BCStkIssue2BO.setForm(request);
		
		System.out.println(reqForm.getDocNo());
		request.setAttribute("docNoFind", reqForm.getDocNo().substring(9));
		
//		BCStkRequestBean  stkReq = BCStkRequestBO.setRequestObject(reqForm);
		
		BCStkIssue2  stkReq = BCStkIssue2BO.setRequestObject(reqForm, request);
		
		BCStkIssue2DAO dao = new BCStkIssue2DAO();
		dao.updateStatus(stkReq.getDocNo(),"4");//ใส่ ittl_status เป็น 4 เมื่อยืนยันรับของ
		 
		String user 	 = (String)request.getSession().getAttribute("userName");
		System.out.println(CommonUtils.getDateTimeForStamp()+" #user:"+user+" #use :BCStkIssue2DAO #mode :updateStatus");					
		
		@SuppressWarnings("unused")
		String[] item_of_service = {"A-01", "A-02", "C-3",  "D-03", "D-04", "D-4",  "E-04", "E-05", "E-4",  "E-5",  "E-S-E",
				"F-04", "F-05", "F-06", "F-4",  "F-5",  "F-6",  "G-06", "G-08", "G-10", "G-11", "G-12", 
				"G-13", "G-14", "G-24", "GE",   "H-07", "H-08", "H-15", "HO",   "I-10", "I-16", "IM",
				"INT",  "J-11", "J-17", "K-14", "K-15", "K-16", "K-17", "K-18", "L-18", "L-19", "M-19",
				"M-21", "N-20", "N-22", "O-21", "P-22", "P-23", "Q-23", "R-24", "R-25", "S-25", "SE", "TREEX", "TRECO", 
				"SER-006","SES","ST",   "T-26", "U-27", "V-28", "Z-01", "Z-02", "Z-03", "Z-04", "Z-05", "Z-06"};
		
		if(request.getParameter("fgDocNo")!=null) {
			// สร้างรายการใหม่ลงใน table: BCFinishGoods (เพื่อเก็บไว้ออก report)
			BCFinishGoodsBean fg = setBCFishGoods(request,stkReq);
			
			new BCFinishGoodsDAO().add(fg);
	//		BCStkIssueBean issue = setBCStkIssue(request,stkReq);
	//		BCStkIssueDAO dao3 = new BCStkIssueDAO();
	//		System.out.println("add stkissue: "+dao3.add(issue));
	//
			System.out.println(CommonUtils.getDateTimeForStamp()+" #user:"+user+" #use :BCFinishGoodsDAO #mode :add "+
					  "#docNo:"+fg.getDocNo());
			
			
			// ใส่ข้อมูล itemCode รายการนั้นใน table: processStock 
			ProcessStockDAO p = new ProcessStockDAO();
	    	Iterator it = fg.getBcFinishGoodsSubs().iterator();
	    	
	        while(it.hasNext()) {
	        	BCFinishGoodsSubBean sub = (BCFinishGoodsSubBean)it.next();
	        	
	        	ProcessStockBean bean = new ProcessStockBean();
	        	bean.setItemCode(sub.getItemCode());
	        	bean.setDocDate(sub.getDocDate());
	        	bean.setProcessFlag(0);
	        	bean.setFlowStatus(1);
	        	bean.setProcessType(2);
	        	bean.setProcessCase(1);// สำหรับ BCStkIssue บันทึกเอกสารให้ =1, ลบเอกสารให้=3
	        	
	        	p.add(bean);
	        	
	    		System.out.println(CommonUtils.getDateTimeForStamp()+" #user:"+user+" #use :ProcessStockDAO #mode :add "+
	  				  "#docNo:"+fg.getDocNo() + " #item:"+sub.getItemCode());        	
	        } 
		}
         
		return true;
		
	}	

	@SuppressWarnings("unchecked")
	private BCFinishGoodsBean setBCFishGoods(HttpServletRequest request,
			BCStkIssue2 stkReq) {
		
		BCFinishGoodsBean fg = new BCFinishGoodsBean();
		
		fg.setDocNo((String)request.getParameter("fgDocNo"));
		fg.setDocDate(CommonUtils.convertStringToDate((String)request.getParameter("fgDocDate")));		
		fg.setDepartCode(stkReq.getDepartCode());		
				
		try {
			fg.setMyDescription(new String((""+(String)request.getParameter("fgMyDescription")).getBytes("ISO8859_1"),"UTF-8")+", "+stkReq.getDocNo());							
		}catch (Exception e) {
			
		}
										
		fg.setIsConfirm(0);
				
		fg.setGlFormat("");//ไม่ต้องบันทึกบัญชี 2pk, 3gs, 4os
		fg.setGlStartPosting(1);
		fg.setIsCancel(0);
		fg.setIsPostGL(0);
		fg.setGlTransData(1);
		fg.setIsProcessOK(0);		
		fg.setIsCompleteSave(1);								
		fg.setProjectCode(stkReq.getProjectCode());		
		fg.setCreatorCode(stkReq.getCreatorCode());				
		fg.setCreateDateTime(new Date());								
		
		HashSet s = new HashSet();		
		
		Iterator it =   stkReq.getBcStkIssueSub2s().iterator();
		
		double tempAmount = 0d;
	    while(it.hasNext()) {
	    	
	    	BCStkIssueSub2 sub2 = (BCStkIssueSub2)it.next();
	    	BCFinishGoodsSubBean sub = new BCFinishGoodsSubBean();
	    	
	    	sub.setMyType(1);
//	    	sub.setItemCode(sub2.getItemCode());
	    	sub.setItemCode(sub2.getIttl_itemCode_ttl2());
	    	sub.setDocDate(fg.getDocDate());
	    	sub.setDepartCode(fg.getDepartCode());	    	
	    	sub.setWhCode("20");
	    	sub.setShelfCode("20");	    		    	
	    	sub.setQty(Double.valueOf(""+sub2.getQty()));
	    	System.out.println("\n\n\n@@@@@@@ ="+sub2.getPrice());
	    	sub.setCost(Double.valueOf(""+sub2.getPrice()));
	    	sub.setSumOfCost(sub.getQty()*sub.getCost());
	    	sub.setAmount(sub.getSumOfCost());
	    	
	    	sub.setUnitCode(sub2.getUnitCode());
	    	sub.setProjectCode(sub2.getProjectCode());	    	
	    	sub.setTransState(0);
	    	sub.setIsCancel(0);
	    	sub.setLineNumber(sub2.getLineNumber());	    	
	    	sub.setItemName(sub2.getItemName());
	    		    	
	    	sub.setBcFinishGoods(fg);
	    	
	    	s.add(sub);
	    	tempAmount += sub.getAmount();
	    }
	    fg.setSumOfAmount(tempAmount);
		fg.setBcFinishGoodsSubs(s);
		
		return fg;
	}

	@SuppressWarnings("unchecked")
	private BCFinishGoodsBean setBCFishGoods(HttpServletRequest request,
			BCStkRequestBean stkReq) {

		BCFinishGoodsBean fg = new BCFinishGoodsBean();
		
		fg.setDocNo((String)request.getParameter("fgDocNo"));
		fg.setDocDate(CommonUtils.convertStringToDate((String)request.getParameter("fgDocDate")));		
		fg.setDepartCode(stkReq.getDepartCode());		
				
		try {
			fg.setMyDescription(new String((""+(String)request.getParameter("fgMyDescription")).getBytes("ISO8859_1"),"UTF-8")+", "+stkReq.getDocNo());							
		}catch (Exception e) {
			
		}
										
		fg.setIsConfirm(0);
				
		fg.setGlFormat("");//ไม่ต้องบันทึกบัญชี 2pk, 3gs, 4os
		fg.setGlStartPosting(1);
		fg.setIsCancel(0);
		fg.setIsPostGL(0);
		fg.setGlTransData(1);
		fg.setIsProcessOK(0);		
		fg.setIsCompleteSave(1);								
		fg.setProjectCode(stkReq.getProjectCode());		
		fg.setCreatorCode(stkReq.getCreatorCode());				
		fg.setCreateDateTime(new Date());								
		
		HashSet s = new HashSet();		
		
		Iterator it =   stkReq.getBcStkRequestSubs().iterator();
		
		double tempAmount = 0d;
	    while(it.hasNext()) {
	    	
	    	BCStkRequestSubBean sub2 = (BCStkRequestSubBean)it.next();
	    	BCFinishGoodsSubBean sub = new BCFinishGoodsSubBean();
	    	
	    	sub.setMyType(1);
//	    	sub.setItemCode(sub2.getItemCode());
	    	sub.setItemCode(sub2.getIttl_itemCode_ttl2());
	    	sub.setDocDate(fg.getDocDate());
	    	sub.setDepartCode(fg.getDepartCode());	    	
	    	sub.setWhCode("20");
	    	sub.setShelfCode("20");	    		    	
	    	sub.setQty(Double.valueOf(""+sub2.getQty()));
	    	sub.setCost(Double.valueOf(""+sub2.getPrice()));
	    	sub.setSumOfCost(sub.getQty()*sub.getCost());
	    	sub.setAmount(sub.getSumOfCost());
	    	
//	    	sub.setUnitCode(sub2.getBcitemUnit().getCode());
	    	sub.setUnitCode(sub2.getUnitCode());
	    	
	    	sub.setProjectCode(sub2.getProjectCode());	    	
	    	sub.setTransState(0);
	    	sub.setIsCancel(0);
	    	sub.setLineNumber(sub2.getLineNumber());	    	
	    	sub.setItemName(sub2.getItemName());
	    		    	
	    	sub.setBcFinishGoods(fg);
	    	
	    	s.add(sub);
	    	tempAmount += sub.getAmount();
	    }
	    fg.setSumOfAmount(tempAmount);
		fg.setBcFinishGoodsSubs(s);
		
		return fg;
	}

	@SuppressWarnings("unchecked")
	public void find(HttpServletRequest request, String docNo) {
		List list = new BCFinishGoodsDAO().find("FROM BCFinishGoodsBean WHERE myDescription like '%"+docNo+"%'");
		
		if(list!=null && list.size()>0) {
			BCFinishGoodsBean fg = (BCFinishGoodsBean)list.get(0);
			
			request.setAttribute("fgDocNo", fg.getDocNo());
			request.setAttribute("fgDocDate", fg.getDocDate());
			request.setAttribute("fgMyDescription", fg.getMyDescription());
		}		
	}

	@SuppressWarnings("unchecked")
	public boolean cancelToFinishGoods_TTL2_stkReq(HttpServletRequest request,
			HttpServletResponse response) {
		
//		BCStkRequestForm reqForm = BCStkRequestBO.setForm(request);
		BCStkIssue2Form reqForm = BCStkIssue2BO.setForm(request);
		
		System.out.println(reqForm.getDocNo());
		request.setAttribute("docNoFind", reqForm.getDocNo().substring(9));
		
//		BCStkRequestBean  stkReq = BCStkRequestBO.setRequestObject(reqForm);
		
		BCStkIssue2  stkReq = BCStkIssue2BO.setRequestObject(reqForm, request);
		
		BCStkIssue2DAO dao = new BCStkIssue2DAO();
		dao.updateStatus(stkReq.getDocNo(),"1");//ให้คงสถานะเป็น จ่ายแล้ว
		 
		String user 	 = (String)request.getSession().getAttribute("userName");
		System.out.println(CommonUtils.getDateTimeForStamp()+" #user:"+user+" #use :BCStkIssue2DAO #mode :updateStatus");					
		
		@SuppressWarnings("unused")
		String[] item_of_service = {"A-01", "A-02", "C-3",  "D-03", "D-04", "D-4",  "E-04", "E-05", "E-4",  "E-5",  "E-S-E",
				"F-04", "F-05", "F-06", "F-4",  "F-5",  "F-6",  "G-06", "G-08", "G-10", "G-11", "G-12", 
				"G-13", "G-14", "G-24", "GE",   "H-07", "H-08", "H-15", "HO",   "I-10", "I-16", "IM",
				"INT",  "J-11", "J-17", "K-14", "K-15", "K-16", "K-17", "K-18", "L-18", "L-19", "M-19",
				"M-21", "N-20", "N-22", "O-21", "P-22", "P-23", "Q-23", "R-24", "R-25", "S-25", "SE", "TREEX", "TRECO", 
				"SER-006","SES","ST",   "T-26", "U-27", "V-28", "Z-01", "Z-02", "Z-03", "Z-04", "Z-05", "Z-06"};
		
		if(request.getParameter("fgDocNo")!=null ) {
			// สร้างรายการใหม่ลงใน table: BCFinishGoods (เพื่อเก็บไว้ออก report)
			BCFinishGoodsBean fg = setBCFishGoods(request,stkReq);
			
			new BCFinishGoodsDAO().add(fg);
	//		BCStkIssueBean issue = setBCStkIssue(request,stkReq);
	//		BCStkIssueDAO dao3 = new BCStkIssueDAO();
	//		System.out.println("add stkissue: "+dao3.add(issue));
	//
			System.out.println(CommonUtils.getDateTimeForStamp()+" #user:"+user+" #use :BCFinishGoodsDAO #mode :add "+
					  "#docNo:"+fg.getDocNo());
			
			
			// ใส่ข้อมูล itemCode รายการนั้นใน table: processStock 
			ProcessStockDAO p = new ProcessStockDAO();
	    	Iterator it = fg.getBcFinishGoodsSubs().iterator();
	    	
	        while(it.hasNext()) {
	        	BCFinishGoodsSubBean sub = (BCFinishGoodsSubBean)it.next();
	        	
	        	ProcessStockBean bean = new ProcessStockBean();
	        	bean.setItemCode(sub.getItemCode());
	        	bean.setDocDate(sub.getDocDate());
	        	bean.setProcessFlag(0);
	        	bean.setFlowStatus(1);
	        	bean.setProcessType(2);
	        	bean.setProcessCase(1);// สำหรับ BCStkIssue บันทึกเอกสารให้ =1, ลบเอกสารให้=3
	        	
	        	p.add(bean);
	        	
	    		System.out.println(CommonUtils.getDateTimeForStamp()+" #user:"+user+" #use :ProcessStockDAO #mode :add "+
	  				  "#docNo:"+fg.getDocNo() + " #item:"+sub.getItemCode());        	
	        } 
		}
         
		return true;
	}

	
}
